﻿using System;
using System.Collections.Generic;

namespace BackToBack
{
    /// <summary>
    /// State of the back-to-back session
    /// </summary>
    public enum BackToBackState
    {
        /// <summary>
        /// One of the sessions is in idle state
        /// </summary>
        Idle,

        /// <summary>
        /// back-to-back session is ready to pass messages 
        /// back and forth between the two sessions
        /// </summary>
        Connected,

        /// <summary>
        /// Some operation is going on
        /// </summary>
        Connecting,

        /// <summary>
        /// back-to-back session is terminating
        /// </summary>
        Terminating,

        /// <summary>
        /// back-to-back session has been terminated
        /// </summary>
        Terminated,
    }

    /// <summary>
    /// Interface for BackToBack strongly typed SDP signaling sessions
    /// </summary>
    internal interface IBackToBackCall
    {
        event EventHandler<BackToBackStateChangedEventArgs> StateChanged;

        BackToBackState State { get; }

        /// <summary>
        /// Begins the participate
        /// </summary>
        /// <param name="userCallback"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        IAsyncResult BeginParticipate(string destinationUri,AsyncCallback userCallback, object state);

        /// <summary>
        /// Ends the participate
        /// </summary>
        /// <param name="result"></param>
        void EndParticipate(IAsyncResult result);

        /// <summary>
        /// Begins the terminate
        /// </summary>
        /// <param name="userCallback"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        IAsyncResult BeginTerminate(AsyncCallback userCallback, object state);

        /// <summary>
        /// Ends the terminate
        /// </summary>
        /// <param name="result"></param>
        void EndTerminate(IAsyncResult result);
    }

    public class BackToBackStateChangedEventArgs : EventArgs
    {
        private readonly BackToBackState _oldState;
        private readonly BackToBackState _newState;

        internal BackToBackStateChangedEventArgs(BackToBackState oldState, BackToBackState newState)
        {
            _oldState = oldState;
            _newState = newState;
        }

        internal BackToBackState OldState
        {
            get { return _oldState; }
        }

        internal BackToBackState NewState
        {
            get { return _newState; }
        }
    }
}
